#include "leestring.h"

namespace yunshan
{
	LeeString::LeeString(const char * str)
	{
		m_strBuf = new StringBuf(str);
	}
	LeeString::LeeString(LeeString & string)
	{
		this->m_strBuf = string.m_strBuf;
		this->m_strBuf->AddRef();
	}
	LeeString::~LeeString()
	{
		if(this->m_strBuf != NULL)
		this->m_strBuf->Release();
	}
	LeeString& LeeString::operator +=(const LeeString& s2)
	{
		if(s2.m_strBuf != NULL && s2.m_strBuf->m_strLen > 0)
		{
			if(m_strBuf !=NULL)
			{
			StringBuf *temp = &(*this->m_strBuf + *s2.m_strBuf);
			this->m_strBuf->Release();
			this->m_strBuf = temp;
			}
			else
			{
			this->m_strBuf = s2.m_strBuf;
			this->m_strBuf->AddRef();
			}
		}
		return *this;
	}
LeeString& LeeString::operator = (const LeeString & str)
	{
		if(this->m_strBuf !=NULL)
		{
			this->m_strBuf->Release();
		}
		this->m_strBuf = str.m_strBuf;
		this->m_strBuf->AddRef();
		return *this;
	}
LeeString& LeeString::operator = (const char* str)
{
	if(this->m_strBuf !=NULL)
	{
		this->m_strBuf->Release();
	}

	this->m_strBuf = new StringBuf(str);
	return *this;
}
int LeeString::find(const char c, int startpos)
{
	int index=-1;
	if(m_strBuf==NULL || m_strBuf->m_strLen == 0)
	return index;
	char* const temp = m_strBuf->m_str;
	for(int i = startpos; i < m_strBuf->m_strLen; i++)
	{
		if(temp[i]==c)
		{
			index=i;
			break;
		}
	}
	return index;
}

LeeString LeeString::substring(int start, int len)
{
	if(start + len > getLength())
	throw "out of range";

	StringBuf* buf = new StringBuf(m_strBuf->m_str+start, len);
	LeeString s;
	s.m_strBuf = buf;

	return s;	
}

}



